package com.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.mapper.StudentInfoMapper;
import com.forum.pojo.UserTipoffInfo;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.UserTipoffInfo;
import com.forum.pojo.vo.UserTipOffVo;
import com.forum.service.UserTipoffInfoService;
import com.forum.mapper.UserTipoffInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86152
* @description 针对表【user_tipoff_info】的数据库操作Service实现
* @createDate 2023-02-21 16:21:21
*/
@Service
public class UserTipoffInfoServiceImpl extends ServiceImpl<UserTipoffInfoMapper, UserTipoffInfo>
    implements UserTipoffInfoService{
    @Autowired
    UserTipoffInfoMapper userTipoffInfoMapper;
    @Autowired
    StudentInfoMapper studentInfoMapper;
    @Override
    public void saveTipOff(Long userId,Long informeduserId,String description) {
        UserTipoffInfo userTipoffInfo=new UserTipoffInfo();
        userTipoffInfo.setUserId(userId);
        userTipoffInfo.setInformedUserId(informeduserId);
        userTipoffInfo.setDescription(description);
        userTipoffInfoMapper.insert(userTipoffInfo);

        StudentInfo studentInfo = studentInfoMapper.selectById(userId);
        studentInfo.setIllegalcount(studentInfo.getIllegalcount()+1);
        studentInfoMapper.updateById(studentInfo);

    }

    @Override
    public Page<UserTipoffInfo> getTipOff(Integer pageNo, Integer pageSize) {
        List<UserTipoffInfo> userTipoffInfos = userTipoffInfoMapper.selectList(null);
        List<UserTipOffVo> result=new ArrayList<>();
        for (UserTipoffInfo userTipoff:userTipoffInfos) {
            UserTipOffVo userTipOffVo=new UserTipOffVo();
            QueryWrapper<StudentInfo> queryWrapper=new QueryWrapper<>();
            QueryWrapper<StudentInfo> wrapper1=new QueryWrapper<>();

            //获得举报人名字
            wrapper1.eq("studentId",userTipoff.getUserId());
            //获得被举报人的名字
            queryWrapper.eq("studentId", userTipoff.getInformedUserId());
            StudentInfo studentInfo = studentInfoMapper.selectOne(queryWrapper);

            userTipOffVo.setTioffId(userTipoff.getTipoffId());
            userTipOffVo.setStates(studentInfo.getStates());
            userTipOffVo.setTipOffCount(studentInfo.getIllegalcount());
            userTipOffVo.setDesc(userTipoff.getDescription());
            userTipOffVo.setUserId(studentInfo.getStudentid());
            userTipOffVo.setNickName(studentInfo.getNickname());
            userTipOffVo.setUserNickName(studentInfoMapper.selectOne(wrapper1).getNickname());
            result.add(userTipOffVo);
        }
        return getPages(pageNo,pageSize,result);
    }
    public static Page getPages(Integer currentPage, Integer pageSize, List list){

        Page page = new Page();

        int size = list.size();
        if(size==0){
            return null;
        }
        if(pageSize > size){
            pageSize = size;
        }

        //求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? (size / pageSize) : (size / pageSize) + 1;

        if(currentPage > maxPage){
            currentPage = maxPage;
        }

        //当前页第一条数据下标
        int curIds = currentPage > 1 ? (currentPage -1) * pageSize : 0;

        List pageList = new ArrayList<>();

        //将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIds + i < size; i++) {
            pageList.add(list.get(curIds + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);

        return page;

    }
}




